/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byzantine.controls;

import byzantine.ByzantineModel;
import byzantine.controls.listeners.SliderListener;
import byzantine.views.View;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

/**
 *
 * @author rpathan
 */
public class StepControls extends JPanel implements View {

    //private int width;
    //private int height;
    private int width = 750;
    private int height = 85;
    
    ByzantineModel model;
    private JLabel panelName;
    private JSlider stepSlider;

    public StepControls(ByzantineModel model) {
        this.model = model;
        model.addObserver(this);
/*
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension screenSize = t.getScreenSize();
        width = (int)((screenSize.width - 10) * 0.55);
        height = (int)((screenSize.height - 100) * 0.11);
*/
        initializeView();
    }

    private void initializeView() {

        panelName = new JLabel("Step");
        panelName.setAlignmentX(Component.CENTER_ALIGNMENT);
        panelName.setAlignmentY(Component.CENTER_ALIGNMENT);
        panelName.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));

        stepSlider = new JSlider(0, 1, 0);
        stepSlider.setAlignmentX(Component.CENTER_ALIGNMENT);
        stepSlider.setAlignmentY(Component.CENTER_ALIGNMENT);
        stepSlider.setPreferredSize(new Dimension((int)(width * .95), (int)(height * .95)));
        stepSlider.setLabelTable(stepSlider.createStandardLabels(1, 0));
        stepSlider.setFont(new Font("Monospaced", Font.PLAIN, 12));
        stepSlider.setSnapToTicks(true);
        stepSlider.setMajorTickSpacing(1);
        stepSlider.setPaintLabels(true);
        stepSlider.setPaintTicks(true);
        stepSlider.setEnabled(false);
        stepSlider.addChangeListener(new SliderListener(model));
       
        Box box1 = Box.createVerticalBox();
        box1.add(panelName);
        box1.add(Box.createVerticalStrut(5));
        box1.add(stepSlider);

        this.add(box1);
        
        setBorder(BorderFactory.createEtchedBorder());
    }

    public void updateSlider() {
        if (model.isSimulationRunning()) {
            stepSlider.setEnabled(true);
            stepSlider.setMaximum(model.getNumSteps() - 1);
        } else {
            stepSlider.setEnabled(false);
            stepSlider.setValue(0);
            stepSlider.setMaximum(1);
        }
    }

    public void valueChanged() {
        if (model.hasSimulationStatusChanged() ||
                model.hasExaminedStepChanged()) {
            updateSlider();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }
}
